<template>
  <!-- 图片拖动缩放 -->
    <div class="wrap" @mousewheel.prevent="rollImg">
        <img :src="url" alt ref="image" @mousedown.prevent="moveImg" />
    </div>
</template>
<script setup>
import { ref } from 'vue'
const url = ref("/src/assets/img/8.jpg")

const image = ref(null)
const rollImg = (e) => {
    let transform = image.value.style.transform
    let zoom = transform.indexOf("scale") != -1 ? +transform.split("(")[1].split(")")[0] : 1
    zoom += e.wheelDelta / 1200
    if (zoom > 0.1 && zoom < 2) {
        image.value.style.transform = "scale(" + zoom + ")"
    }
}

const moveImg = (e) => {
    let x = e.clientX
    let y = e.clientY
    let left = x - e.target.offsetLeft
    let top = y - e.target.offsetTop
    document.onmousemove = (ev) => {
        e.target.style.left = ev.clientX - left + 'px'
        e.target.style.top = ev.clientY - top + 'px'
    }
    document.onmouseup = () => {
        document.onmousemove = null
    }
}
</script>

<style lang='scss' scoped>
.wrap {
    position: relative;
    width: 100%;
    height: 100%;
    overflow: hidden;
    display: flex;
    align-items: center;
    justify-content: center;
    background-color: aqua;
}
img {
    position: absolute;
    cursor: move;
}
</style>
